package uk.co.biggroup.lotto.syndicate.dataset;

import java.io.Serializable;
import java.util.ArrayList;

import org.htmlcleaner.TagNode;
import org.htmlcleaner.XPatherException;

import uk.co.biggroup.lotto.syndicate.Logh;


public class DataDraws implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 7526472295633776147L;
	//<draws amount='0.00' accountstatus='3' desc='Status: PURPLE - user has no funds but there are no open draws to be added to at the moment. Action: None.' updated='18-06-2012 17:09:37' >
	private float amount;  
	private int accountstatus;
	private String shortStatus;
	private String desc;
	private String updated;
	private ArrayList<DataDraw> items;
	
	private static final String DRAWS_AMOUNT = "//draws/@amount";
	private static final String DRAWS_ACCSTATUS = "//draws/@accountstatus";
	private static final String DRAWS_UPDATED = "//draws/@updated";
	private static final String DRAWS_DESC = "//draws/@desc";
	
	private static final String DRAWS_DRAW_ID = "//draws/draw/@id";
	private static final String DRAWS_DRAW_ORDER = "//draws/draw/@order";
	private static final String DRAWS_DRAW_DATE = "//draws/draw/@date";
	private static final String DRAWS_DRAW_STATUS = "//draws/draw/@status";
	private static final String DRAWS_DRAW_TYPE = "//draws/draw/@type";	
	private static final String DRAWS_DRAW_RESULT = "//draws/draw/@result";

	@SuppressWarnings("unused")
	private static final String ERROR_XPATH = "//error/@msg";
	
	public DataDraws(TagNode node) {
		try {
		this.initDraws(node);
		this.initDraw(node);
		} catch (XPatherException e) {
			Logh.i("XPatherException");
		}
	}
	
	private void initDraws(TagNode node) throws XPatherException {
		Object[] nodes_amount = node.evaluateXPath(DRAWS_AMOUNT);
		Object[] nodes_accountstatus = node.evaluateXPath(DRAWS_ACCSTATUS);
		Object[] nodes_desc = node.evaluateXPath(DRAWS_DESC);
		Object[] nodes_updated = node.evaluateXPath(DRAWS_UPDATED);
		this.desc = nodes_desc[0].toString();
		this.updated = nodes_updated[0].toString();
		this.amount = Float.parseFloat(nodes_amount[0].toString());
		this.accountstatus = Integer.parseInt(nodes_accountstatus[0].toString());
		switch (this.accountstatus) {
		case 1:
			this.shortStatus = "OK - IN GAME ";
			break;
		case 2:
			this.shortStatus = "NOT INCLUDED ";
			break;
		case 3:
			this.shortStatus = "TOP UP BALANCE! ";
			break;
		case 4:
			this.shortStatus = "OUT OF THE GAME ";
			break;
		}
	}
	
	private void initDraw(TagNode node) throws XPatherException {
		Object[] nodes_id = node.evaluateXPath(DRAWS_DRAW_ID);
		Object[] nodes_order = node.evaluateXPath(DRAWS_DRAW_ORDER);
		Object[] nodes_date = node.evaluateXPath(DRAWS_DRAW_DATE);
		Object[] nodes_status = node.evaluateXPath(DRAWS_DRAW_STATUS);
		Object[] nodes_type = node.evaluateXPath(DRAWS_DRAW_TYPE);
		Object[] nodes_result = node.evaluateXPath(DRAWS_DRAW_RESULT);
		int objects = nodes_id.length;
		items = new ArrayList<DataDraw>(objects);
		for(int i = 0; i <= (objects - 1); i++ ) {
			DataDraw draw = new DataDraw(node,
					Integer.parseInt(nodes_id[i].toString()),
					Integer.parseInt(nodes_order[i].toString()),
					nodes_date[i].toString(),
					nodes_status[i].toString(),
					nodes_type[i].toString(),
					nodes_result[i].toString());
			items.add(draw);
		}
		
	}
	
	public int getStatus() {
		return this.accountstatus;
	}
	
	public String getDescr() {
		return this.desc;
	}
	
	public String getShortStatus() {
		return this.shortStatus;
	}

	public String getUpdated() {
		return this.updated;
	}
	
	public String getBalance() {
		return Float.toString(this.amount);
	}
	
	public ArrayList<DataDraw> getDataDraw() {
		return this.items;
	}
}

